/*
 * @Author jiangzhiwei
 * @Date 2023-07-23 16:50:42
 * @LastEditors jiangzhiwei
 * @LastEditTime 2023-07-28 14:10:19
 * @FilePath \test-cron\src\components\yc-cron\components\panes\mixins\panemixin.js
 * @Description 面板混入
 */
import { emitValue } from '../../../utils/componentutil'
import { VALUE_NAME, VALUE_EMIT_NAME, EXPRESSION_EMIT_NAME, PARSE_ERROR_EMIT_NAME } from '../../../utils/constant'
import { h } from '../../../utils/domutil'

export default {
  props: {
    [VALUE_NAME]: {
      type: String,
      default: undefined
    },
    /** cron表达式类型linux/spring/quarts */
    type: {
      type: String,
      default: undefined
    }
  },
  data () {
    return {
      /** 选中列表的序号 */
      checkedValue: undefined,
      /** 各个选项的配置信息 */
      options: []
    }
  },
  methods: {
    /** 选项发生变化 */
    onInput (value) {
      this.checkedValue = value
    },
    /** cron表达式发生变化 */
    onExpressionChange (expression) {
      emitValue(this, expression)
      this.afterExpressionChange(expression)
    },
    /** 解析错误事件 */
    onParseError (message) {
      emitValue(this, message, PARSE_ERROR_EMIT_NAME)
    },
    afterExpressionChange () {},
    /** 根据选项配置信息生成选项结点 */
    generateOptionNodes () {
      return this.options.map((item, index) => {
        return h(
          item.label,
          {
            props: {
              [VALUE_NAME]: this.checkedValue,
              label: index,
              descriptions: this.descriptions[index],
              expression: this[VALUE_NAME],
              ...item.props
            },
            on: {
              [VALUE_EMIT_NAME]: this.onInput,
              [EXPRESSION_EMIT_NAME]: this.onExpressionChange,
              [PARSE_ERROR_EMIT_NAME]: this.onParseError
            }
          }
        )
      })
    }
  }
}